﻿using System;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace Win7RovMobileApp
{
    public partial class ValabilityMenu : PhoneApplicationPage
    {
        public String[] ImageSources;
        public String[] ImageTags;

        private double ImageWidth           = 160;	    // Image width
        private double ImageHeight          = 100;	    // Image height

        private double Target               = 0;		// Target moving position
        private double Current              = 0;	    // Current position
        private double Sponge               = 0;		// Temp used to store last moving
        private double AppearDisappearPoint = 1.5;	    // Define when the image disappear and reappear
        private double ReturnSpeed          = 0.5;		// Control the Return Speed
        private double BounceSpeed          = 0.5;		// Control the bounce Speed
        private double SCALE_DOWN           = 0.7;		// Scale down ratio
        private double SCALE_MAX            = 2;		// Maximum scale for the first image
        private double SPACE_SPAN           = 300;		// Used to compute the margin between image
        private double OFFSET_X             = 50;		// Apply for all images
        private int    SelectedPeriod       = 3;        // Index counter for car images

        private static int FPS              = 24;       // fps of the on enter frame event
        private DispatcherTimer RepositioningTimer;     // on enter frame simulator


        public ValabilityMenu()
        {
            InitializeComponent();
            GetSpecificPictures();
            DataContext = App.UserPreferences;
            App.UserPreferences.Valabilities.Clear();
            Height = 768; Width = 480;
            
            // add images to client view menu
            AddImages();

            // start the image repositioning system
            RepositioningTimer = new DispatcherTimer();
            RepositioningTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000 / FPS);
            RepositioningTimer.Tick += new EventHandler(_timer_Tick);
            RepositioningTimer.Start();
        }


        private void GetSpecificPictures()
        {
            // specific images for specific vehicle
            switch (App.UserPreferences.VehicleCategories[App.UserPreferences.VehicleType])
            {
                case "AUTO":
                {
                    ImageSources = new String[]{"../Images/7zile_auto.png", "../Images/30zile_auto.png", "../Images/3luni_auto.png", "../Images/12luni_auto.png"};
                    ImageTags    = new String[] { "3 euro", "7 euro", "13 euro", "28 euro" }; 
                    break;
                }
                case "TR_MF_0_35":
                {
                    ImageSources = new String[] { "../Images/7zile_tm1.png", "../Images/30zile_tm1.png", "../Images/3luni_tm1.png", "../Images/12luni_tm1.png" };
                    ImageTags    = new String[] { "6 euro", "16 euro", "36 euro", "96 euro" }; 
                    break;
                }
                case "TR_MF_35_75":
                {
                    ImageSources = new String[] { "../Images/7zile_tm2.png", "../Images/30zile_tm2.png", "../Images/3luni_tm2.png", "../Images/12luni_tm2.png" };
                    ImageTags    = new String[] { "20 euro", "52 euro", "120 euro", "320 euro" }; 
                    break;
                }
                case "TR_MF_75_120":
                {
                    ImageSources = new String[] { "../Images/7zile_tm3.png", "../Images/30zile_tm3.png", "../Images/3luni_tm3.png", "../Images/12luni_tm3.png" };
                    ImageTags    = new String[] { "35 euro", "91 euro", "210 euro", "560 euro" }; 
                    break;
                }
                case "TR_MF_MTMA_3":
                {
                    ImageSources = new String[] { "../Images/7zile_tm4.png", "../Images/30zile_tm4.png", "../Images/3luni_tm4.png", "../Images/12luni_tm4.png" };
                    ImageTags    = new String[] { "45 euro", "117 euro", "270 euro", "720 euro" }; 
                    break;
                }
                case "TR_MF_MTMA_4":
                {
                    ImageSources = new String[] { "../Images/7zile_tm5.png", "../Images/30zile_tm5.png", "../Images/3luni_tm5.png", "../Images/12luni_tm5.png" };
                    ImageTags    = new String[] { "55 euro", "143 euro", "374 euro", "1210 euro" }; 
                    break;
                }
            }
        }


        private void AddImages()
        {
            for (int i = 0; i < ImageSources.Length; i++)
            {
                //create new valability term
                Image validPeriod = new Image();
                validPeriod.Source = new BitmapImage(new Uri(ImageSources[i], UriKind.RelativeOrAbsolute));
                switch (i)
                {
                    case 0:
                    {
                        validPeriod.Name = "7 zile";
                        validPeriod.Tag = ImageTags[0];
                        App.UserPreferences.Valabilities.Add(validPeriod.Name, "7ZI");
                        break;
                    }
                    case 1:
                    {
                        validPeriod.Name = "30 zile";
                        validPeriod.Tag = ImageTags[1];
                        App.UserPreferences.Valabilities.Add(validPeriod.Name, "30ZI");
                        break;
                    }
                    case 2:
                    {
                        validPeriod.Name = "3 luni";
                        validPeriod.Tag = ImageTags[2];
                        App.UserPreferences.Valabilities.Add(validPeriod.Name, "90ZI");
                        break;
                    }
                    case 3:
                    {
                        validPeriod.Name = "12 luni";
                        validPeriod.Tag = ImageTags[3];
                        App.UserPreferences.Valabilities.Add(validPeriod.Name, "12LU");
                        break;
                    }
                }

                //add to view
                MenuPanel.Children.Add(validPeriod);                
            }
        }


        // Reposition an image
        private void RepositionImage(Image image, int index, int total)
        {
            double offset = (index - Current) % total;
            if (offset < -AppearDisappearPoint)
            {
                offset = offset % total + total;
            }
            else if (offset > total - AppearDisappearPoint)
            {
                offset -= total;
            }

            // scale and position the image according to their index and current position
            double scale = Math.Pow(SCALE_DOWN, offset);
            image.Width = ImageWidth * SCALE_MAX * scale;
            image.Height = ImageHeight * SCALE_MAX * scale;
            image.Opacity = 0.8;

            image.SetValue(Canvas.LeftProperty, OFFSET_X + Width / 2 - image.Width / 2 + SPACE_SPAN * (1 - scale));
            image.SetValue(Canvas.TopProperty, Height / 2 - image.Height / 2);
            
            // sort the Children according to their Width
            image.SetValue(Canvas.ZIndexProperty, (int)Math.Round(image.Width));
        }


        // reposition the images
        void _timer_Tick(object sender, EventArgs e)
        {
            RepositionAllImages();

            // compute the current position
            // added sponge effect
            Sponge = (Target - Current) * ReturnSpeed + Sponge * BounceSpeed;
            Current += Sponge;
        }


        // Reposition all images
        private void RepositionAllImages()
        {
            for (int i = 0; i < MenuPanel.Children.Count; i++)
            {
                RepositionImage(MenuPanel.Children[i] as Image, i, MenuPanel.Children.Count);
            }
        }


        private void MoveImages_Click(object sender, MouseButtonEventArgs e)
        {
            Target++;
            SelectedPeriod++;
            SelectedPeriod %= ImageSources.Length; 
        }


        private void MenuPanel_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            App.UserPreferences.Valability = (MenuPanel.Children[SelectedPeriod] as Image).Name as String;
            App.UserPreferences.TotalCosts = (MenuPanel.Children[SelectedPeriod] as Image).Tag as String;
            NavigationService.Navigate(new Uri("/Menus/InvoiceDetails.xaml", UriKind.Relative));
        }

    }
}